﻿using Demo.Interface;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using PracticalDemo.Utility.Filter;
using System;

namespace PracticalDemo.Controllers
{
    /// <summary>
    /// 面向环形编程   独创
    /// Filter MVC 流程，流程外处理不了(404)
    /// 中间件 任何请求最外层到达，提前做些筛选过滤工作。但是中间件一般没有控制器action这些信息，不适合业务逻辑
    /// Autofac 可以深入到业务逻辑层做扩展
    /// 
    /// filter
    /// 1 IResourceFilter-OnResourceExecuting发生在控制器实例化之前
    /// 2 IActionFilter-OnActionExecuting
    /// 3 IActionFilter-OnActionExecuted
    /// 4 IResultFilter-OnResultExecuting
    /// 5 IResultFilter-OnResultExecuted   Result是视图替换环节
    /// 6 IResourceFilter-OnResultExecuted 发生在最后
    /// 
    /// 不同环境都可以做缓存，只是效果不一样
    /// 当然是IResourceFilter，
    /// 
    /// 基于IResourceFilter完成了缓存，避免了控制器实例化和Action执行，但是视图重新执行了
    /// 是以Action为单位，不能细化到业务逻辑层
    /// 
    /// 多个步骤，某个步骤需要缓存结果，Filter不适合的
    /// 基于autofac的AOP缓存了service层的结果
    /// 
    /// 怎么样可以不执行视图，或者直接重用视图的结果，直接重用HTML？
    /// ResponseCache:在请求响应式，添加了一个responseheader，来指导浏览器缓存结果
    /// 
    /// 地址栏回车、链接跳转、新开页面、前进后退、F5刷新、Ctrl+F5 浏览器对缓存会有不同的行为而且不同的浏览器效果还不一样
    /// 
    /// 中间件怎么缓存？
    /// 结合ResponseCache，在中间件完成拦截，可以完全不进入MVC
    /// 
    /// </summary>
    public class FifthController : Controller
    {
        #region Identity
        private readonly ILogger<FifthController> _logger;
        private readonly ITestServiceA _iTestServiceA;
        private readonly ITestServiceB _iTestServiceB;
        private readonly ITestServiceC _iTestServiceC;
        private readonly ITestServiceD _iTestServiceD;
        private readonly ITestServiceE _iTestServiceE;
        private readonly IA _ia;
        public FifthController(ILogger<FifthController> logger
            , ITestServiceA testServiceA
             , ITestServiceB testServiceB
            , ITestServiceC testServiceC
            , ITestServiceD testServiceD
            , ITestServiceE testServiceE
            , IA a)
        {
            this._logger = logger;
            this._iTestServiceA = testServiceA;
            this._iTestServiceB = testServiceB;
            this._iTestServiceC = testServiceC;
            this._iTestServiceD = testServiceD;
            this._iTestServiceE = testServiceE;
            this._ia = a;
        }
        #endregion

        #region Index
        [CustomActionFilterAttribute]
        [CustomResultFilterAttribute]
        [CustomResourceFilterAttribute]
        public IActionResult Index()
        {
            Console.WriteLine($"【{DateTime.Now}】This is Index Action");

            return View();
        }
        #endregion

        //[CustomCacheResourceFilterAttribute]
        //[ResponseCache(Duration = 60)]
        //[CustomCacheResultFilter(Duration = 700)]

        [ResponseCache(Duration = 600)]//缓存客户端，配合服务端缓存
        public IActionResult Info()
        {
            //base.HttpContext.Response.Headers["Cache-Control"] = "public,max-age=600";

            base.ViewBag.Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
            base.ViewBag.ServiceNow = this._ia.PlusTime(123, 234);

            return View();
        }

        public IActionResult Infomation()
        {
            Console.WriteLine($"【{DateTime.Now}】This is Infomation Action");
            this._ia.Show(123, "简简单单");
            return View();
        }
    }
}
